
package org.openbravo.erpWindows.ConstructionProject;

import org.openbravo.erpCommon.utility.*;
import org.openbravo.erpCommon.reference.*;
import org.openbravo.erpCommon.ad_actionButton.*;
import org.openbravo.data.FieldProvider;
import org.openbravo.utils.FormatUtilities;
import org.openbravo.erpCommon.info.LocationSearchData;
import org.openbravo.base.secureApp.HttpSecureAppServlet;
import org.openbravo.base.secureApp.VariablesSecureApp;
import org.openbravo.erpCommon.businessUtility.WindowTabs;
import org.openbravo.erpCommon.businessUtility.WindowTabsData;
import org.openbravo.utils.Replace;
import org.openbravo.xmlEngine.XmlDocument;
import org.openbravo.data.UtilSql;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// imports para transacciones
import org.openbravo.database.ConnectionProvider;
import java.sql.Connection;

import org.openbravo.erpCommon.utility.DateTimeData;

public class ConstructionSite extends HttpSecureAppServlet {
  protected static final String windowId = "800001";
  protected static final String tabId = "800002";
  protected static final String tableLevel = "1";
  protected static final String defaultTabView = "RELATION";
  protected static final double SUBTABS_COL_SIZE = 15;
  protected TableSQLData tableSQL = null;

  public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
    VariablesSecureApp vars = new VariablesSecureApp(request);

    if (!Utility.hasAccess(this, vars, tableLevel, Utility.getContext(this, vars, "#AD_Client_ID", windowId), Utility.getContext(this, vars, "#AD_Org_ID", windowId), windowId, tabId)) {
      bdError(response, "AccessTableNoView", vars.getLanguage());
      return;
    }

    try {
      tableSQL = new TableSQLData(vars, this, tabId, Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId));
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    String strOrderBy = vars.getSessionValue(tabId + "|orderby");
    if (!strOrderBy.equals("")) {
      vars.setSessionValue(tabId + "|newOrder", "1");
    }

    if (vars.commandIn("DEFAULT")) {

      String strC_Project_ID = vars.getGlobalVariable("inpcProjectId", windowId + "|C_Project_ID", "");
      

      String strView = vars.getSessionValue(tabId + "|ConstructionSite.view");
      if (strView.equals("")) {
        strView = defaultTabView;

        if (strView.equals("EDIT")) {
          if (strC_Project_ID.equals("")) strC_Project_ID = firstElement(vars);
          if (strC_Project_ID.equals("")) strView = "RELATION";
        }
      }
      if (strView.equals("EDIT")) 

        printPageEdit(response, request, vars, false, strC_Project_ID);

      else printPageDataSheet(response, vars, strC_Project_ID);
    } else if (vars.commandIn("DIRECT")) {
      String strC_Project_ID = vars.getStringParameter("inpDirectKey");
      if (strC_Project_ID.equals("")) strC_Project_ID = vars.getRequiredGlobalVariable("inpcProjectId", windowId + "|C_Project_ID");
      else vars.setSessionValue(windowId + "|C_Project_ID", strC_Project_ID);
      
      vars.setSessionValue(tabId + "|ConstructionSite.view", "EDIT");

      printPageEdit(response, request, vars, false, strC_Project_ID);

    } else if (vars.commandIn("TAB")) {


      String strView = vars.getSessionValue(tabId + "|ConstructionSite.view");
      String strC_Project_ID = "";
      if (strView.equals("")) {
        strView = defaultTabView;
        if (strView.equals("EDIT")) {
          strC_Project_ID = firstElement(vars);
          if (strC_Project_ID.equals("")) strView = "RELATION";
        }
      }
      if (strView.equals("EDIT")) {

        if (strC_Project_ID.equals("")) strC_Project_ID = firstElement(vars);
        printPageEdit(response, request, vars, false, strC_Project_ID);

      } else printPageDataSheet(response, vars, "");
    } else if (vars.commandIn("SEARCH")) {
      String strParamC_BPartner_ID = vars.getRequestGlobalVariable("inpParamC_BPartner_ID", tabId + "|paramC_BPartner_ID");

      //vars.setSessionValue(tabId + "|paramSessionDate", Utility.getContext(this, vars, "#Date", windowId));
      //vars.setSessionValue(tabId + "|paramSessionDate", Utility.getTransactionalDate(this, vars, windowId));
      
      
      vars.removeSessionValue(windowId + "|C_Project_ID");
      String strC_Project_ID="";

      String strView = vars.getSessionValue(tabId + "|ConstructionSite.view");
      if (strView.equals("")) strView=defaultTabView;

      if (strView.equals("EDIT")) {
        strC_Project_ID = firstElement(vars);
        if (strC_Project_ID.equals("")) strView = "RELATION";
      }

      if (strView.equals("EDIT")) 

        printPageEdit(response, request, vars, false, strC_Project_ID);

      else printPageDataSheet(response, vars, strC_Project_ID);
    } else if (vars.commandIn("RELATION")) {
      

      String strC_Project_ID = vars.getGlobalVariable("inpcProjectId", windowId + "|C_Project_ID", "");
      vars.setSessionValue(tabId + "|ConstructionSite.view", "RELATION");
      printPageDataSheet(response, vars, strC_Project_ID);
    } else if (vars.commandIn("NEW")) {


      printPageEdit(response, request, vars, true, "");

    } else if (vars.commandIn("EDIT")) {

      String strC_Project_ID = vars.getGlobalVariable("inpcProjectId", windowId + "|C_Project_ID", "");
      vars.setSessionValue(tabId + "|ConstructionSite.view", "EDIT");

      setHistoryCommand(request, "EDIT");
      printPageEdit(response, request, vars, false, strC_Project_ID);

    } else if (vars.commandIn("NEXT")) {

      String strC_Project_ID = vars.getRequiredStringParameter("inpcProjectId");
      
      String strNext = nextElement(vars, strC_Project_ID);

      printPageEdit(response, request, vars, false, strNext);
    } else if (vars.commandIn("PREVIOUS")) {

      String strC_Project_ID = vars.getRequiredStringParameter("inpcProjectId");
      
      String strPrevious = previousElement(vars, strC_Project_ID);

      printPageEdit(response, request, vars, false, strPrevious);
    } else if (vars.commandIn("FIRST_RELATION")) {

      vars.setSessionValue(tabId + "|ConstructionSite.initRecordNumber", "0");
      response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
    } else if (vars.commandIn("PREVIOUS_RELATION")) {

      String strInitRecord = vars.getSessionValue(tabId + "|ConstructionSite.initRecordNumber");
      String strRecordRange = Utility.getContext(this, vars, "#RecordRange", windowId);
      int intRecordRange = strRecordRange.equals("")?0:Integer.parseInt(strRecordRange);
      if (strInitRecord.equals("") || strInitRecord.equals("0")) {
        vars.setSessionValue(tabId + "|ConstructionSite.initRecordNumber", "0");
      } else {
        int initRecord = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
        initRecord -= intRecordRange;
        strInitRecord = ((initRecord<0)?"0":Integer.toString(initRecord));
        vars.setSessionValue(tabId + "|ConstructionSite.initRecordNumber", strInitRecord);
      }
      vars.removeSessionValue(windowId + "|C_Project_ID");

      response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
    } else if (vars.commandIn("NEXT_RELATION")) {

      String strInitRecord = vars.getSessionValue(tabId + "|ConstructionSite.initRecordNumber");
      String strRecordRange = Utility.getContext(this, vars, "#RecordRange", windowId);
      int intRecordRange = strRecordRange.equals("")?0:Integer.parseInt(strRecordRange);
      int initRecord = (strInitRecord.equals("")?0:Integer.parseInt(strInitRecord));
      if (initRecord==0) initRecord=1;
      initRecord += intRecordRange;
      strInitRecord = ((initRecord<0)?"0":Integer.toString(initRecord));
      vars.setSessionValue(tabId + "|ConstructionSite.initRecordNumber", strInitRecord);
      vars.removeSessionValue(windowId + "|C_Project_ID");

      response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
    } else if (vars.commandIn("FIRST")) {

      String strC_Project_ID = vars.getRequiredStringParameter("inpcProjectId");
      
      String strFirst = firstElement(vars);

      printPageEdit(response, request, vars, false, strFirst);
    } else if (vars.commandIn("LAST_RELATION")) {

      String strLast = lastElement(vars);
      printPageDataSheet(response, vars, strLast);
    } else if (vars.commandIn("LAST")) {

      String strC_Project_ID = vars.getRequiredStringParameter("inpcProjectId");
      
      String strLast = lastElement(vars);

      printPageEdit(response, request, vars, false, strLast);
    } else if (vars.commandIn("SAVE_NEW_RELATION", "SAVE_NEW_NEW", "SAVE_NEW_EDIT")) {

      OBError myError = null;
      ConstructionSiteData data = null;
      int total = 0;
      try {
        data = getEditVariables(vars);
        String strSequence = SequenceIdData.getSequence(this, "C_Project", vars.getClient());
        log4j.info("Sequence: " + strSequence);
        data.cProjectId = strSequence;
        total = data.insert(this);
      } catch(ServletException ex) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
        if (!myError.isConnectionAvailable()) {
          bdErrorConnection(response);
          return;
        } else vars.setMessage(tabId, myError);
      }
      if (myError==null && total==0) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=DBExecuteError");
        vars.setMessage(tabId, myError);
      }
      if (myError!=null) {
        if (data!=null) {
          data.cProjectId = "";
          vars.setEditionData(tabId, data);
        }
        response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW");
      } else {
        if (myError==null) {
          myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=RowsInserted");
          myError.setMessage(total + " " + myError.getMessage());
          vars.setMessage(tabId, myError);
        }
        vars.setSessionValue(windowId + "|C_Project_ID", data.cProjectId);
        if (vars.commandIn("SAVE_NEW_NEW")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW");
        else if (vars.commandIn("SAVE_NEW_EDIT")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
        else response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
      }
    } else if (vars.commandIn("SAVE_EDIT_RELATION", "SAVE_EDIT_NEW", "SAVE_EDIT_EDIT", "SAVE_EDIT_NEXT")) {

      String strC_Project_ID = vars.getRequiredGlobalVariable("inpcProjectId", windowId + "|C_Project_ID");
      int total = 0;
      ConstructionSiteData data = null;
      OBError myError = null;
      try {
        data = getEditVariables(vars);
        total = data.update(this);
      } catch(ServletException ex) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
        if (!myError.isConnectionAvailable()) {
          bdErrorConnection(response);
          return;
        } else vars.setMessage(tabId, myError);
      }
      if (myError==null && total==0) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=DBExecuteError");
        vars.setMessage(tabId, myError);
      }
      if (myError!=null) {
        if (data!=null) {
          
          vars.setEditionData(tabId, data);
        }
        response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
      } else {
        if (myError==null) {
          myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=RowsUpdated");
          myError.setMessage(total + " " + myError.getMessage());
          vars.setMessage(tabId, myError);
        }
        if (vars.commandIn("SAVE_EDIT_NEW")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=NEW");
        else if (vars.commandIn("SAVE_EDIT_EDIT")) response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
        else if (vars.commandIn("SAVE_EDIT_NEXT")) {
          String strNext = nextElement(vars, strC_Project_ID);
          vars.setSessionValue(windowId + "|C_Project_ID", strNext);
          response.sendRedirect(strDireccion + request.getServletPath() + "?Command=EDIT");
        } else response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELATION");
      }
/*    } else if (vars.commandIn("DELETE_RELATION")) {

      String strC_Project_ID = vars.getRequiredInStringParameter("inpcProjectId");
      String message = deleteRelation(vars, strC_Project_ID);
      if (!message.equals("")) {
        bdError(response, message, vars.getLanguage());
      } else {
        vars.removeSessionValue(windowId + "|cProjectId");
        vars.setSessionValue(tabId + "|ConstructionSite.view", "RELATION");
        response.sendRedirect(strDireccion + request.getServletPath());
      }*/
    } else if (vars.commandIn("DELETE")) {

      String strC_Project_ID = vars.getRequiredStringParameter("inpcProjectId");
      //ConstructionSiteData data = getEditVariables(vars);
      int total = 0;
      OBError myError = null;
      try {
        total = ConstructionSiteData.delete(this, strC_Project_ID);
      } catch(ServletException ex) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
        if (!myError.isConnectionAvailable()) {
          bdErrorConnection(response);
          return;
        } else vars.setMessage(tabId, myError);
      }
      if (myError==null && total==0) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=DBExecuteError");
        vars.setMessage(tabId, myError);
      }
      vars.removeSessionValue(windowId + "|cProjectId");
      vars.setSessionValue(tabId + "|ConstructionSite.view", "RELATION");
      if (myError==null) {
        myError = Utility.translateError(this, vars, vars.getLanguage(), "@CODE=RowsDeleted");
        myError.setMessage(total + " " + myError.getMessage());
        vars.setMessage(tabId, myError);
      }
      response.sendRedirect(strDireccion + request.getServletPath());
    } else if (vars.commandIn("BUTTONChangeProjectStatus800002")) {
        String strC_Project_ID = vars.getGlobalVariable("inpcProjectId", windowId + "|C_Project_ID", "");
        String strchangeprojectstatus = vars.getStringParameter("inpchangeprojectstatus");
        String strProcessing = vars.getStringParameter("inpprocessing", "Y");
        String strprojectstatus = vars.getRequiredStringParameter("inpprojectstatus");

        printPageButtonChangeProjectStatus800002(response, vars, strC_Project_ID, strchangeprojectstatus, strProcessing, strprojectstatus);
    } else if (vars.commandIn("BUTTONGenerateOrder800005")) {
        String strC_Project_ID = vars.getGlobalVariable("inpcProjectId", windowId + "|C_Project_ID", "");
        String strgenerateorder = vars.getStringParameter("inpgenerateorder");
        String strProcessing = vars.getStringParameter("inpprocessing", "Y");
        
        printPageButtonGenerateOrder800005(response, vars, strC_Project_ID, strgenerateorder, strProcessing);

    } else if (vars.commandIn("SAVE_BUTTONChangeProjectStatus800002")) {
        String strC_Project_ID = vars.getGlobalVariable("inpKey", windowId + "|C_Project_ID", "");
        String strchangeprojectstatus = vars.getStringParameter("inpchangeprojectstatus");
        String strprojectstatus = vars.getRequiredStringParameter("inpprojectstatus");

        String strProcessing = vars.getStringParameter("inpprocessing");
        OBError myMessage = null;
        try {
          String pinstance = SequenceIdData.getSequence(this, "AD_PInstance", vars.getClient());
          PInstanceProcessData.insertPInstance(this, pinstance, "800002", (("C_Project_ID".equalsIgnoreCase("AD_Language"))?"0":strC_Project_ID), strProcessing, vars.getUser(), vars.getClient(), vars.getOrg());
          PInstanceProcessData.insertPInstanceParam(this, pinstance, "0", "ChangeProjectStatus", strchangeprojectstatus, vars.getClient(), vars.getOrg(), vars.getUser());

          
          ActionButtonData.process800002(this, pinstance);

          PInstanceProcessData[] pinstanceData = PInstanceProcessData.select(this, pinstance);
          String messageResult="";
          myMessage = Utility.getProcessInstanceMessage(this, vars, pinstanceData);
        } catch (ServletException ex) {
          myMessage = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
          if (!myMessage.isConnectionAvailable()) {
            bdErrorConnection(response);
            return;
          } else vars.setMessage(tabId, myMessage);
        }
        if (myMessage!=null) {
          if (log4j.isDebugEnabled()) log4j.debug(myMessage.getMessage());
          vars.setMessage(tabId, myMessage);
        }
        printPageClosePopUp(response, vars);
    } else if (vars.commandIn("SAVE_BUTTONGenerateOrder800005")) {
        String strC_Project_ID = vars.getGlobalVariable("inpKey", windowId + "|C_Project_ID", "");
        String strgenerateorder = vars.getStringParameter("inpgenerateorder");
        
        String strProcessing = vars.getStringParameter("inpprocessing");
        OBError myMessage = null;
        try {
          String pinstance = SequenceIdData.getSequence(this, "AD_PInstance", vars.getClient());
          PInstanceProcessData.insertPInstance(this, pinstance, "800005", (("C_Project_ID".equalsIgnoreCase("AD_Language"))?"0":strC_Project_ID), strProcessing, vars.getUser(), vars.getClient(), vars.getOrg());
          
          
          ActionButtonData.process800005(this, pinstance);

          PInstanceProcessData[] pinstanceData = PInstanceProcessData.select(this, pinstance);
          String messageResult="";
          myMessage = Utility.getProcessInstanceMessage(this, vars, pinstanceData);
        } catch (ServletException ex) {
          myMessage = Utility.translateError(this, vars, vars.getLanguage(), ex.getMessage());
          if (!myMessage.isConnectionAvailable()) {
            bdErrorConnection(response);
            return;
          } else vars.setMessage(tabId, myMessage);
        }
        if (myMessage!=null) {
          if (log4j.isDebugEnabled()) log4j.debug(myMessage.getMessage());
          vars.setMessage(tabId, myMessage);
        }
        printPageClosePopUp(response, vars);





    } else if (vars.getCommand().toUpperCase().startsWith("BUTTON") || vars.getCommand().toUpperCase().startsWith("SAVE_BUTTON")) {
      pageErrorPopUp(response);
    } else pageError(response);
  }
/*
  String deleteRelation(VariablesSecureApp vars, String strC_Project_ID) throws IOException, ServletException {
    log4j.debug("Deleting records");
    Connection conn = this.getTransactionConnection();
    try {
      if (strC_Project_ID.startsWith("(")) strC_Project_ID = strC_Project_ID.substring(1, strC_Project_ID.length()-1);
      if (!strC_Project_ID.equals("")) {
        strC_Project_ID = Replace.replace(strC_Project_ID, "'", "");
        StringTokenizer st = new StringTokenizer(strC_Project_ID, ",", false);
        while (st.hasMoreTokens()) {
          String strKey = st.nextToken();
          if (ConstructionSiteData.deleteTransactional(conn, this, strKey)==0) {
            releaseRollbackConnection(conn);
            log4j.warn("deleteRelation - key :" + strKey + " - 0 records deleted");
          }
        }
      }
      releaseCommitConnection(conn);
    } catch (ServletException e) {
      releaseRollbackConnection(conn);
      e.printStackTrace();
      log4j.error("Rollback in transaction");
      return "ProcessRunError";
    }
    return "";
  }
*/
  ConstructionSiteData getEditVariables(VariablesSecureApp vars) throws IOException,ServletException {
    ConstructionSiteData data = new ConstructionSiteData();
    data.adClientId = vars.getRequiredGlobalVariable("inpadClientId", windowId + "|AD_Client_ID");    data.adClientIdr = vars.getStringParameter("inpadClientId_R");    data.adOrgId = vars.getRequiredGlobalVariable("inpadOrgId", windowId + "|AD_Org_ID");    data.adOrgIdr = vars.getStringParameter("inpadOrgId_R");    data.value = vars.getStringParameter("inpvalue");    data.name = vars.getRequiredStringParameter("inpname");    data.isactive = vars.getStringParameter("inpisactive", "N");    data.description = vars.getStringParameter("inpdescription");    data.note = vars.getStringParameter("inpnote");    data.projectstatus = vars.getRequiredStringParameter("inpprojectstatus");    data.projectstatusr = vars.getStringParameter("inpprojectstatus_R");    data.projectphase = vars.getStringParameter("inpprojectphase");    data.projectphaser = vars.getStringParameter("inpprojectphase_R");    data.projectkind = vars.getStringParameter("inpprojectkind");    data.projectkindr = vars.getStringParameter("inpprojectkind_R");    data.publicprivate = vars.getStringParameter("inppublicprivate");    data.publicprivater = vars.getStringParameter("inppublicprivate_R");    data.cLocationId = vars.getStringParameter("inpcLocationId");    data.cLocationIdr = vars.getStringParameter("inpcLocationId_R");    data.salesrepId = vars.getStringParameter("inpsalesrepId");    data.salesrepIdr = vars.getStringParameter("inpsalesrepId_R");    data.cBpartnerId = vars.getStringParameter("inpcBpartnerId");    data.cBpartnerIdr = vars.getStringParameter("inpcBpartnerId_R");    data.cBpartnerLocationId = vars.getStringParameter("inpcBpartnerLocationId");    data.cBpartnerLocationIdr = vars.getStringParameter("inpcBpartnerLocationId_R");    data.adUserId = vars.getStringParameter("inpadUserId");    data.adUserIdr = vars.getStringParameter("inpadUserId_R");    data.billtoId = vars.getStringParameter("inpbilltoId");    data.billtoIdr = vars.getStringParameter("inpbilltoId_R");    data.invoiceToproject = vars.getStringParameter("inpinvoiceToproject", "N");    data.mPricelistId = vars.getStringParameter("inpmPricelistId");    data.mPricelistIdr = vars.getStringParameter("inpmPricelistId_R");    data.cCurrencyId = vars.getRequiredStringParameter("inpcCurrencyId");    data.cCurrencyIdr = vars.getStringParameter("inpcCurrencyId_R");    data.accountno = vars.getStringParameter("inpaccountno");    data.documentCopies = vars.getStringParameter("inpdocumentCopies");    data.paymentrule = vars.getStringParameter("inppaymentrule");    data.paymentruler = vars.getStringParameter("inppaymentrule_R");    data.cPaymenttermId = vars.getStringParameter("inpcPaymenttermId");    data.cPaymenttermIdr = vars.getStringParameter("inpcPaymenttermId_R");    data.mWarehouseId = vars.getStringParameter("inpmWarehouseId");    data.mWarehouseIdr = vars.getStringParameter("inpmWarehouseId_R");    data.datecontract = vars.getStringParameter("inpdatecontract");    data.datefinish = vars.getStringParameter("inpdatefinish");    data.plannedamt = vars.getRequiredStringParameter("inpplannedamt");    data.invoicedamt = vars.getRequiredStringParameter("inpinvoicedamt");    data.changeprojectstatus = vars.getStringParameter("inpchangeprojectstatus");    data.generateorder = vars.getStringParameter("inpgenerateorder");    data.poreference = vars.getStringParameter("inpporeference");    data.cCampaignId = vars.getStringParameter("inpcCampaignId");    data.setprojecttype = vars.getStringParameter("inpsetprojecttype");    data.generateto = vars.getStringParameter("inpgenerateto");    data.cProjecttypeId = vars.getStringParameter("inpcProjecttypeId");    data.invoicedqty = vars.getRequiredStringParameter("inpinvoicedqty");    data.projectcategory = vars.getStringParameter("inpprojectcategory");    data.plannedmarginamt = vars.getRequiredStringParameter("inpplannedmarginamt");    data.committedqty = vars.getRequiredStringParameter("inpcommittedqty");    data.plannedpoamt = vars.getStringParameter("inpplannedpoamt");    data.lastplannedproposaldate = vars.getStringParameter("inplastplannedproposaldate");    data.mPricelistVersionId = vars.getStringParameter("inpmPricelistVersionId");    data.issummary = vars.getStringParameter("inpissummary", "N");    data.committedamt = vars.getRequiredStringParameter("inpcommittedamt");    data.cProjectId = vars.getRequestGlobalVariable("inpcProjectId", windowId + "|C_Project_ID");    data.copyfrom = vars.getStringParameter("inpcopyfrom");    data.iscommitceiling = vars.getStringParameter("inpiscommitceiling", "N");    data.processed = vars.getStringParameter("inpprocessed", "N");    data.cPhaseId = vars.getStringParameter("inpcPhaseId");    data.iscommitment = vars.getRequiredInputGlobalVariable("inpiscommitment", windowId + "|IsCommitment", "N");    data.plannedqty = vars.getRequiredStringParameter("inpplannedqty");    data.projectbalanceamt = vars.getRequiredStringParameter("inpprojectbalanceamt");    data.processing = vars.getStringParameter("inpprocessing");
      data.createdby = vars.getUser();
      data.updatedby = vars.getUser();


          vars.getGlobalVariable("inppreprojectcategory", windowId + "|PREPROJECTCATEGORY", "");
    
    

          if (data.value.equals("")) data.value = Utility.getDocumentNo(this, vars.getClient(), "C_Project", true);

    return data;
  }

   ConstructionSiteData[] getRelationData(ConstructionSiteData[] data) {
    if (data!=null) {
      for (int i=0;i<data.length;i++) {        data[i].adClientId = FormatUtilities.truncate(data[i].adClientId, 14);        data[i].adOrgId = FormatUtilities.truncate(data[i].adOrgId, 14);        data[i].value = FormatUtilities.truncate(data[i].value, 20);        data[i].name = FormatUtilities.truncate(data[i].name, 50);        data[i].description = FormatUtilities.truncate(data[i].description, 50);        data[i].note = FormatUtilities.truncate(data[i].note, 50);        data[i].projectstatus = FormatUtilities.truncate(data[i].projectstatus, 10);        data[i].projectphase = FormatUtilities.truncate(data[i].projectphase, 10);        data[i].projectkind = FormatUtilities.truncate(data[i].projectkind, 2);        data[i].publicprivate = FormatUtilities.truncate(data[i].publicprivate, 2);        data[i].cLocationId = FormatUtilities.truncate(data[i].cLocationId, 50);        data[i].salesrepId = FormatUtilities.truncate(data[i].salesrepId, 22);        data[i].cBpartnerId = FormatUtilities.truncate(data[i].cBpartnerId, 40);        data[i].cBpartnerLocationId = FormatUtilities.truncate(data[i].cBpartnerLocationId, 50);        data[i].adUserId = FormatUtilities.truncate(data[i].adUserId, 14);        data[i].billtoId = FormatUtilities.truncate(data[i].billtoId, 50);        data[i].mPricelistId = FormatUtilities.truncate(data[i].mPricelistId, 22);        data[i].cCurrencyId = FormatUtilities.truncate(data[i].cCurrencyId, 14);        data[i].accountno = FormatUtilities.truncate(data[i].accountno, 20);        data[i].paymentrule = FormatUtilities.truncate(data[i].paymentrule, 1);        data[i].cPaymenttermId = FormatUtilities.truncate(data[i].cPaymenttermId, 22);        data[i].mWarehouseId = FormatUtilities.truncate(data[i].mWarehouseId, 40);        data[i].poreference = FormatUtilities.truncate(data[i].poreference, 20);        data[i].cCampaignId = FormatUtilities.truncate(data[i].cCampaignId, 14);        data[i].cProjecttypeId = FormatUtilities.truncate(data[i].cProjecttypeId, 22);        data[i].projectcategory = FormatUtilities.truncate(data[i].projectcategory, 1);        data[i].mPricelistVersionId = FormatUtilities.truncate(data[i].mPricelistVersionId, 22);        data[i].cPhaseId = FormatUtilities.truncate(data[i].cPhaseId, 22);}
    }
    return data;
  }



    void refreshSessionEdit(VariablesSecureApp vars, FieldProvider[] data) {
      if (data==null || data.length==0) return;
          vars.setSessionValue(windowId + "|AD_Client_ID", data[0].getField("adClientId"));    vars.setSessionValue(windowId + "|AD_Org_ID", data[0].getField("adOrgId"));    vars.setSessionValue(windowId + "|C_Project_ID", data[0].getField("cProjectId"));    vars.setSessionValue(windowId + "|IsCommitment", data[0].getField("iscommitment"));
    }

    void refreshSessionNew(VariablesSecureApp vars) throws IOException,ServletException {
      ConstructionSiteData[] data = ConstructionSiteData.selectEdit(this, vars.getLanguage(), vars.getStringParameter("inpcProjectId", ""));
      if (data==null || data.length==0) return;
      refreshSessionEdit(vars, data);
    }

  String nextElement(VariablesSecureApp vars, String strSelected) throws IOException, ServletException {
    if (strSelected == null || strSelected.equals("")) return firstElement(vars);
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting next element");
        e.printStackTrace();
      }
      if (data!=null) {
        boolean found = false;
        for (int i=0;i<data.length;i++) {
          if (!found && data[i].getField("ID").equals(strSelected)) found=true;
          else if (found) return data[i].getField("ID");
        }
      }
    }
    return strSelected;
  }

  int getKeyPosition(VariablesSecureApp vars, String strSelected) throws IOException, ServletException {
    if (log4j.isDebugEnabled()) log4j.debug("getKeyPosition: " + strSelected);
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting key position");
        e.printStackTrace();
      }
      if (data!=null) {
        for (int i=0;i<data.length;i++) {
          if (log4j.isDebugEnabled()) log4j.debug("row: " + i + " - key: " + data[i].getField("ID"));
          if (data[i].getField("ID").equals(strSelected)) return i;
        }
      }
    }
    return 0;
  }

  String previousElement(VariablesSecureApp vars, String strSelected) throws IOException, ServletException {
    if (strSelected == null || strSelected.equals("")) return firstElement(vars);
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting previous element");
        e.printStackTrace();
      }
      if (data!=null) {
        String previous = strSelected;
        for (int i=0;i<data.length;i++) {
          if (data[i].getField("ID").equals(strSelected)) return previous;
          previous = data[i].getField("ID");
        }
      }
    }
    return strSelected;
  }

  String firstElement(VariablesSecureApp vars) throws IOException, ServletException {
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,1);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting first element");
        e.printStackTrace();
      }
      if (data!=null && data.length!=0) return data[0].getField("ID");
    }
    return "";
  }

  String lastElement(VariablesSecureApp vars) throws IOException, ServletException {
    if (tableSQL!=null) {
      FieldProvider[] data = null;
      try{
        String strSQL = ModelSQLGeneration.generateSQL(this, vars, tableSQL, (tableSQL.getTableName() + "." + tableSQL.getKeyColumn() + " AS ID"), new Vector<String>(), new Vector<String>());
        ExecuteQuery execquery = new ExecuteQuery(this, strSQL, tableSQL.getParameterValues());
        data = execquery.select(0,0);
      } catch (Exception e) { 
        if (log4j.isDebugEnabled()) log4j.debug("Error getting last element");
        e.printStackTrace();
      }
      if (data!=null && data.length!=0) return data[data.length-1].getField("ID");
    }
    return "";
  }

  void printPageDataSheet(HttpServletResponse response, VariablesSecureApp vars, String strC_Project_ID)
    throws IOException, ServletException {
    if (log4j.isDebugEnabled()) log4j.debug("Output: dataSheet");

    String strParamC_BPartner_ID = vars.getSessionValue(tabId + "|paramC_BPartner_ID");

    boolean hasSearchCondition=false;
    vars.removeEditionData(tabId);
    if (!(strParamC_BPartner_ID.equals(""))) hasSearchCondition=true;
    String strOffset = vars.getSessionValue(tabId + "|offset");
    String selectedRow = "0";
    if (!strC_Project_ID.equals("")) {
      selectedRow = Integer.toString(getKeyPosition(vars, strC_Project_ID));
    }

    String[] discard={"isNotFiltered","isNotTest"};
    if (hasSearchCondition) discard[0] = new String("isFiltered");
    if (vars.getSessionValue("#ShowTest", "N").equals("Y")) discard[1] = new String("isTest");
    XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpWindows/ConstructionProject/ConstructionSite_Relation", discard).createXmlDocument();

    ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "ConstructionSite", false, "document.frmMain.inpcProjectId", "grid", "..", "".equals("Y"), "ConstructionProject", strReplaceWith, false);
    toolbar.prepareRelationTemplate("N".equals("Y"), hasSearchCondition, vars.getSessionValue("#ShowTest", "N").equals("Y"), "N".equals("Y"));
    xmlDocument.setParameter("toolbar", toolbar.toString());



    String strOrderBy = vars.getSessionValue(tabId + "|orderby");
    StringBuffer orderByArray = new StringBuffer();
    if (!strOrderBy.equals("")) {
      vars.setSessionValue(tabId + "|newOrder", "1");
      String positions = vars.getSessionValue(tabId + "|orderbyPositions");
      orderByArray.append("var orderByPositions = new Array(\n");
      if (!positions.equals("")) {
        StringTokenizer tokens=new StringTokenizer(positions, ",");
        boolean firstOrder = true;
        while(tokens.hasMoreTokens()){
          if (!firstOrder) orderByArray.append(",\n");
          orderByArray.append("\"").append(tokens.nextToken()).append("\"");
          firstOrder = false;
        }
      }
      orderByArray.append(");\n");
      String directions = vars.getSessionValue(tabId + "|orderbyDirections");
      orderByArray.append("var orderByDirections = new Array(\n");
      if (!positions.equals("")) {
        StringTokenizer tokens=new StringTokenizer(directions, ",");
        boolean firstOrder = true;
        while(tokens.hasMoreTokens()){
          if (!firstOrder) orderByArray.append(",\n");
          orderByArray.append("\"").append(tokens.nextToken()).append("\"");
          firstOrder = false;
        }
      }
      orderByArray.append(");\n");
    }

    xmlDocument.setParameter("selectedColumn", "\nvar selectedRow = " + selectedRow + ";\n" + orderByArray.toString());
    xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
    xmlDocument.setParameter("windowId", windowId);
    xmlDocument.setParameter("KeyName", "cProjectId");
    xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
    xmlDocument.setParameter("theme", vars.getTheme());
    //xmlDocument.setParameter("buttonReference", Utility.messageBD(this, "Reference", vars.getLanguage()));
    try {
      WindowTabs tabs = new WindowTabs(this, vars, tabId, windowId);
      xmlDocument.setParameter("parentTabContainer", tabs.parentTabs());
      xmlDocument.setParameter("mainTabContainer", tabs.mainTabs());
      xmlDocument.setParameter("childTabContainer", tabs.childTabs());
      NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "ConstructionSite_Relation.html", "ConstructionProject", "W", strReplaceWith, tabs.breadcrumb());
      xmlDocument.setParameter("navigationBar", nav.toString());
      LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "ConstructionSite_Relation.html", strReplaceWith);
      xmlDocument.setParameter("leftTabs", lBar.relationTemplate());
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
    try {
      KeyMap key = new KeyMap(this, vars, tabId, windowId);
      xmlDocument.setParameter("keyMap", key.getRelationKeyMaps());
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
    {
      OBError myMessage = vars.getMessage(tabId);
      vars.removeMessage(tabId);
      if (myMessage!=null) {
        xmlDocument.setParameter("messageType", myMessage.getType());
        xmlDocument.setParameter("messageTitle", myMessage.getTitle());
        xmlDocument.setParameter("messageMessage", myMessage.getMessage());
      }
    }


    xmlDocument.setParameter("grid", Utility.getContext(this, vars, "#RecordRange", windowId));

    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println(xmlDocument.print());
    out.close();
  }

  void printPageEdit(HttpServletResponse response, HttpServletRequest request, VariablesSecureApp vars,boolean boolNew, String strC_Project_ID)
    throws IOException, ServletException {
    if (log4j.isDebugEnabled()) log4j.debug("Output: edit");
    String strOrderByFilter = vars.getSessionValue(tabId + "|orderby");
    String orderClause = " C_Project.Value";
    if (strOrderByFilter==null || strOrderByFilter.equals("")) strOrderByFilter = orderClause;
    /*{
      if (!strOrderByFilter.equals("") && !orderClause.equals("")) strOrderByFilter += ", ";
      strOrderByFilter += orderClause;
    }*/
    String strCommand = null;
    ConstructionSiteData[] data=null;
    XmlDocument xmlDocument=null;
    FieldProvider dataField = vars.getEditionData(tabId);
    vars.removeEditionData(tabId);
    String strParamC_BPartner_ID = vars.getSessionValue(tabId + "|paramC_BPartner_ID");

    
    
    String strParamFilter="paramFilter";
    if (!(strParamC_BPartner_ID.equals(""))) strParamFilter = "";
    
    boolean hasSearchCondition=false;
    if (!(strParamC_BPartner_ID.equals(""))) hasSearchCondition=true;

       String strParamSessionDate = vars.getGlobalVariable("inpParamSessionDate", Utility.getTransactionalDate(this, vars, windowId), "");
      String strParamUser = vars.getUser();
      String buscador = "";
      String[] discard = {"", "isNotTest"};
      
      if (vars.getSessionValue("#ShowTest", "N").equals("Y")) discard[1] = new String("isTest");
    if (dataField==null) {
      if (!boolNew) {
        discard[0] = new String("newDiscard");
        data = ConstructionSiteData.selectEdit(this, vars.getLanguage(), strC_Project_ID);
  
        refreshSessionEdit(vars, data);
        strCommand = "EDIT";
      }

      if (boolNew || data==null || data.length==0) {
        discard[0] = new String ("editDiscard");
        strCommand = "NEW";
        data = new ConstructionSiteData[0];
      } else {
        discard[0] = new String ("newDiscard");
      }
    } else {
      if (dataField.getField("cProjectId").equals("")) {
        discard[0] = new String ("editDiscard");
        strCommand = "NEW";
        boolNew = true;
      } else {
        discard[0] = new String ("newDiscard");
        strCommand = "EDIT";
      }
    }
    xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpWindows/ConstructionProject/ConstructionSite_Edition",discard).createXmlDocument();

    ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "ConstructionSite", (boolNew || (dataField==null && (data==null || data.length==0))), "document.frmMain.inpcProjectId", "", "..", "".equals("Y"), "ConstructionProject", strReplaceWith, true);
    toolbar.prepareEditionTemplate("N".equals("Y"), hasSearchCondition, vars.getSessionValue("#ShowTest", "N").equals("Y"), "N".equals("Y"));
    xmlDocument.setParameter("toolbar", toolbar.toString());

    try {
      WindowTabs tabs = new WindowTabs(this, vars, tabId, windowId);
      xmlDocument.setParameter("parentTabContainer", tabs.parentTabs());
      xmlDocument.setParameter("mainTabContainer", tabs.mainTabs());
      xmlDocument.setParameter("childTabContainer", tabs.childTabs());
      NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "ConstructionSite_Relation.html", "ConstructionProject", "W", strReplaceWith, tabs.breadcrumb());
      xmlDocument.setParameter("navigationBar", nav.toString());
      LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "ConstructionSite_Relation.html", strReplaceWith);
      xmlDocument.setParameter("leftTabs", lBar.editionTemplate());
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
    try {
      KeyMap key = new KeyMap(this, vars, tabId, windowId);
      xmlDocument.setParameter("keyMap", key.getEditionKeyMaps(boolNew));
    } catch (Exception ex) {
      throw new ServletException(ex);
    }

        String strPREPROJECTCATEGORY = "N";
    vars.setSessionValue(windowId + "|PREPROJECTCATEGORY", strPREPROJECTCATEGORY);
    

    if (dataField==null) {
      if (boolNew || data==null || data.length==0) {
        refreshSessionNew(vars);
        data = ConstructionSiteData.set(Utility.getDefault(this, vars, "AD_Client_ID", "@AD_CLIENT_ID@", "800001", ""), Utility.getDefault(this, vars, "AD_Org_ID", "@AD_Org_ID@", "800001", ""), Utility.getDefault(this, vars, "Value", "", "800001", ""), Utility.getDefault(this, vars, "Name", "", "800001", ""), "Y", Utility.getDefault(this, vars, "Description", "", "800001", ""), Utility.getDefault(this, vars, "Note", "", "800001", ""), Utility.getDefault(this, vars, "Projectstatus", "OP", "800001", ""), Utility.getDefault(this, vars, "Projectphase", "", "800001", ""), Utility.getDefault(this, vars, "Projectkind", "", "800001", ""), Utility.getDefault(this, vars, "Publicprivate", "", "800001", ""), Utility.getDefault(this, vars, "C_Location_ID", "", "800001", ""), ConstructionSiteData.selectDef800142_0(this, Utility.getDefault(this, vars, "C_Location_ID", "", "800001", "")), Utility.getDefault(this, vars, "SalesRep_ID", "", "800001", ""), Utility.getDefault(this, vars, "C_BPartner_ID", "", "800001", ""), ConstructionSiteData.selectDef3902_1(this, Utility.getDefault(this, vars, "C_BPartner_ID", "", "800001", "")), Utility.getDefault(this, vars, "C_BPartner_Location_ID", "", "800001", ""), Utility.getDefault(this, vars, "AD_User_ID", "-1", "800001", ""), Utility.getDefault(this, vars, "BillTo_ID", "", "800001", ""), Utility.getDefault(this, vars, "Invoice_ToProject", "", "800001", "N"), Utility.getDefault(this, vars, "M_PriceList_ID", "", "800001", ""), Utility.getDefault(this, vars, "C_Currency_ID", "", "800001", ""), Utility.getDefault(this, vars, "AccountNo", "", "800001", ""), Utility.getDefault(this, vars, "Document_Copies", "1", "800001", ""), Utility.getDefault(this, vars, "PaymentRule", "", "800001", ""), Utility.getDefault(this, vars, "C_PaymentTerm_ID", "", "800001", ""), Utility.getDefault(this, vars, "M_Warehouse_ID", "", "800001", ""), Utility.getDefault(this, vars, "DateContract", "", "800001", ""), Utility.getDefault(this, vars, "DateFinish", "", "800001", ""), Utility.getDefault(this, vars, "PlannedAmt", "", "800001", "0"), Utility.getDefault(this, vars, "InvoicedAmt", "", "800001", "0"), Utility.getDefault(this, vars, "ChangeProjectStatus", "", "800001", "N"), (vars.getLanguage().equals("en_US")?ListData.selectName(this, "800002", Utility.getDefault(this, vars, "ChangeProjectStatus", "", "800001", "N")):ListData.selectNameTrl(this, vars.getLanguage(), "800002", Utility.getDefault(this, vars, "ChangeProjectStatus", "", "800001", "N"))), Utility.getDefault(this, vars, "GenerateOrder", "", "800001", "N"), Utility.getDefault(this, vars, "POReference", "", "800001", ""), Utility.getDefault(this, vars, "C_Campaign_ID", "", "800001", ""), Utility.getDefault(this, vars, "Setprojecttype", "", "800001", "N"), Utility.getDefault(this, vars, "GenerateTo", "", "800001", "N"), Utility.getDefault(this, vars, "C_ProjectType_ID", "", "800001", ""), Utility.getDefault(this, vars, "InvoicedQty", "", "800001", "0"), Utility.getDefault(this, vars, "ProjectCategory", "@PREPROJECTCATEGORY@", "800001", ""), Utility.getDefault(this, vars, "PlannedMarginAmt", "", "800001", "0"), Utility.getDefault(this, vars, "CommittedQty", "", "800001", "0"), Utility.getDefault(this, vars, "PlannedPOAmt", "", "800001", ""), Utility.getDefault(this, vars, "LastPlannedProposalDate", "", "800001", ""), Utility.getDefault(this, vars, "Created", "", "800001", ""), Utility.getDefault(this, vars, "M_PriceList_Version_ID", "", "800001", ""), Utility.getDefault(this, vars, "IsSummary", "", "800001", "N"), Utility.getDefault(this, vars, "CommittedAmt", "", "800001", "0"), Utility.getDefault(this, vars, "C_Project_ID", "", "800001", "0"), Utility.getDefault(this, vars, "CopyFrom", "", "800001", "N"), Utility.getDefault(this, vars, "IsCommitCeiling", "", "800001", "N"), Utility.getDefault(this, vars, "Processed", "", "800001", "N"), Utility.getDefault(this, vars, "C_Phase_ID", "", "800001", ""), Utility.getDefault(this, vars, "IsCommitment", "", "800001", "N"), Utility.getDefault(this, vars, "PlannedQty", "", "800001", "0"), Utility.getDefault(this, vars, "ProjectBalanceAmt", "", "800001", "0"), Utility.getDefault(this, vars, "Processing", "", "800001", "N"));
        
      }
    }

        xmlDocument.setParameter("PREPROJECTCATEGORY", strPREPROJECTCATEGORY);
    

    xmlDocument.setParameter("commandType", strCommand);
    xmlDocument.setParameter("buscador",buscador);
    xmlDocument.setParameter("windowId", windowId);
    xmlDocument.setParameter("changed", (strCommand.equals("NEW")?"1":""));
    xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
    xmlDocument.setParameter("theme", vars.getTheme());
    //xmlDocument.setParameter("buttonReference", Utility.messageBD(this, "Reference", vars.getLanguage()));

    xmlDocument.setParameter("paramSessionDate", strParamSessionDate);

    xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
    {
      OBError myMessage = vars.getMessage(tabId);
      vars.removeMessage(tabId);
      if (myMessage!=null) {
        xmlDocument.setParameter("messageType", myMessage.getType());
        xmlDocument.setParameter("messageTitle", myMessage.getTitle());
        xmlDocument.setParameter("messageMessage", myMessage.getMessage());
      }
    }
    
    
    if (dataField==null) xmlDocument.setData("structure1",data);
    else {
      FieldProvider[] dataAux = new FieldProvider[1];
      dataAux[0] = dataField;
      xmlDocument.setData("structure1",dataAux);
    }
    try {
      ComboTableData comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "AD_Client_ID", "", "129", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("adClientId"):dataField.getField("adClientId")));
xmlDocument.setData("reportAD_Client_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "AD_Org_ID", "", "130", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("adOrgId"):dataField.getField("adOrgId")));
xmlDocument.setData("reportAD_Org_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "17", "Projectstatus", "800002", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("projectstatus"):dataField.getField("projectstatus")));
xmlDocument.setData("reportProjectstatus","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "17", "Projectphase", "800003", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("projectphase"):dataField.getField("projectphase")));
xmlDocument.setData("reportProjectphase","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "17", "Projectkind", "800004", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("projectkind"):dataField.getField("projectkind")));
xmlDocument.setData("reportProjectkind","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "17", "Publicprivate", "800005", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("publicprivate"):dataField.getField("publicprivate")));
xmlDocument.setData("reportPublicprivate","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "18", "SalesRep_ID", "190", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("salesrepId"):dataField.getField("salesrepId")));
xmlDocument.setData("reportSalesRep_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_BPartner_Location_ID", "", "167", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cBpartnerLocationId"):dataField.getField("cBpartnerLocationId")));
xmlDocument.setData("reportC_BPartner_Location_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "AD_User_ID", "", "123", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("adUserId"):dataField.getField("adUserId")));
xmlDocument.setData("reportAD_User_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "18", "BillTo_ID", "159", "119", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("billtoId"):dataField.getField("billtoId")));
xmlDocument.setData("reportBillTo_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "M_PriceList_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("mPricelistId"):dataField.getField("mPricelistId")));
xmlDocument.setData("reportM_PriceList_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_Currency_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cCurrencyId"):dataField.getField("cCurrencyId")));
xmlDocument.setData("reportC_Currency_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
xmlDocument.setParameter("buttonDocument_Copies", Utility.messageBD(this, "Calc", vars.getLanguage()));
comboTableData = new ComboTableData(vars, this, "17", "PaymentRule", "195", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("paymentrule"):dataField.getField("paymentrule")));
xmlDocument.setData("reportPaymentRule","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "C_PaymentTerm_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("cPaymenttermId"):dataField.getField("cPaymenttermId")));
xmlDocument.setData("reportC_PaymentTerm_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
comboTableData = new ComboTableData(vars, this, "19", "M_Warehouse_ID", "", "", Utility.getContext(this, vars, "#User_Org", windowId), Utility.getContext(this, vars, "#User_Client", windowId), 0);
Utility.fillSQLParameters(this, vars, (dataField==null?data[0]:dataField), comboTableData, windowId, (dataField==null?data[0].getField("mWarehouseId"):dataField.getField("mWarehouseId")));
xmlDocument.setData("reportM_Warehouse_ID","liststructure", comboTableData.select(!strCommand.equals("NEW")));
comboTableData = null;
xmlDocument.setParameter("DateContract_Format", vars.getSessionValue("#AD_SqlDateFormat"));
xmlDocument.setParameter("DateFinish_Format", vars.getSessionValue("#AD_SqlDateFormat"));
xmlDocument.setParameter("buttonPlannedAmt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonInvoicedAmt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonProjectBalanceAmt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonPlannedQty", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonCommittedAmt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("Created_Format", vars.getSessionValue("#AD_SqlDateFormat"));
xmlDocument.setParameter("LastPlannedProposalDate_Format", vars.getSessionValue("#AD_SqlDateFormat"));
xmlDocument.setParameter("buttonPlannedPOAmt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonCommittedQty", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonPlannedMarginAmt", Utility.messageBD(this, "Calc", vars.getLanguage()));
xmlDocument.setParameter("buttonInvoicedQty", Utility.messageBD(this, "Calc", vars.getLanguage()));

    } catch (Exception ex) {
      ex.printStackTrace();
      throw new ServletException(ex);
    }
    response.setContentType("text/html; charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println(xmlDocument.print());
    out.close();
  }

    void printPageButtonChangeProjectStatus800002(HttpServletResponse response, VariablesSecureApp vars, String strC_Project_ID, String strchangeprojectstatus, String strProcessing, String strprojectstatus)
    throws IOException, ServletException {
      log4j.debug("Output: Button process 800002");
      String[] discard = {"newDiscard"};
      response.setContentType("text/html; charset=UTF-8");
      PrintWriter out = response.getWriter();
      XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/ChangeProjectStatus", discard).createXmlDocument();
      xmlDocument.setParameter("key", strC_Project_ID);
      xmlDocument.setParameter("processing", strProcessing);
      xmlDocument.setParameter("form", "ConstructionSite_Edition.html");
      xmlDocument.setParameter("window", windowId);
      xmlDocument.setParameter("css", vars.getTheme());
      xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
      xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
      xmlDocument.setParameter("cancel", Utility.messageBD(this, "Cancel", vars.getLanguage()));
      xmlDocument.setParameter("ok", Utility.messageBD(this, "OK", vars.getLanguage()));
      
      try {
        KeyMap key = new KeyMap(this, vars, tabId, windowId);
        xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps());
      } catch (Exception ex) {
        throw new ServletException(ex);
      }
      
      {
        OBError myMessage = vars.getMessage("800002");
        vars.removeMessage("800002");
        if (myMessage!=null) {
          xmlDocument.setParameter("messageType", myMessage.getType());
          xmlDocument.setParameter("messageTitle", myMessage.getTitle());
          xmlDocument.setParameter("messageMessage", myMessage.getMessage());
        }
      }

      xmlDocument.setParameter("projectstatus", strprojectstatus);
    try {
    ComboTableData comboTableData = null;
    } catch (Exception ex) {
      throw new ServletException(ex);
    }
xmlDocument.setParameter("processId", "800002");
xmlDocument.setParameter("processDescription", "Change project status");
xmlDocument.setParameter("projectaction", strchangeprojectstatus);
FieldProvider[] dataProjectAction = ActionButtonUtility.projectAction(this, vars, strchangeprojectstatus, "800002", strprojectstatus);
xmlDocument.setData("reportprojectaction", "liststructure", dataProjectAction);
StringBuffer dact = new StringBuffer();
if (dataProjectAction!=null) {
  dact.append("var arrProjectAction = new Array(\n");
  for (int i=0;i<dataProjectAction.length;i++) {
    dact.append("new Array(\"" + dataProjectAction[i].getField("id") + "\", \"" + dataProjectAction[i].getField("name") + "\", \"" + dataProjectAction[i].getField("description") + "\")\n");
    if (i<dataProjectAction.length-1) dact.append(",\n");
  }
  dact.append(");");
} else dact.append("var arrProjectAction = null");
xmlDocument.setParameter("array", dact.toString());

      out.println(xmlDocument.print());
      out.close();
    }
    void printPageButtonGenerateOrder800005(HttpServletResponse response, VariablesSecureApp vars, String strC_Project_ID, String strgenerateorder, String strProcessing)
    throws IOException, ServletException {
      log4j.debug("Output: Button process 800005");
      String[] discard = {"newDiscard"};
      response.setContentType("text/html; charset=UTF-8");
      PrintWriter out = response.getWriter();
      XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/GenerateOrder800005", discard).createXmlDocument();
      xmlDocument.setParameter("key", strC_Project_ID);
      xmlDocument.setParameter("processing", strProcessing);
      xmlDocument.setParameter("form", "ConstructionSite_Edition.html");
      xmlDocument.setParameter("window", windowId);
      xmlDocument.setParameter("css", vars.getTheme());
      xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
      xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
      xmlDocument.setParameter("cancel", Utility.messageBD(this, "Cancel", vars.getLanguage()));
      xmlDocument.setParameter("ok", Utility.messageBD(this, "OK", vars.getLanguage()));
      
      try {
        KeyMap key = new KeyMap(this, vars, tabId, windowId);
        xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps());
      } catch (Exception ex) {
        throw new ServletException(ex);
      }
      
      {
        OBError myMessage = vars.getMessage("800005");
        vars.removeMessage("800005");
        if (myMessage!=null) {
          xmlDocument.setParameter("messageType", myMessage.getType());
          xmlDocument.setParameter("messageTitle", myMessage.getTitle());
          xmlDocument.setParameter("messageMessage", myMessage.getMessage());
        }
      }

          try {
    ComboTableData comboTableData = null;
    } catch (Exception ex) {
      throw new ServletException(ex);
    }

      out.println(xmlDocument.print());
      out.close();
    }






  public String getServletInfo() {
    return "Servlet ConstructionSite. This Servlet was made by Wad constructor";
  } // fin del método getServletInfo()
}
